/************** O T T R   C O P Y R I G H T   N O T I C E **************
 **                                                                   **
 ** © 2013 OTTR, Inc.  All rights reserved.                           **
 **                                                                   ** 
 *************** O T T R   C O P Y R I G H T   N O T I C E *************/

package com.jonathan.javatar;

import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author Jonathan Paz <jonathan.paz@ottr.com>
 */
public class PaxTarEntry extends USTarEntry {
    long atime;
    String charset;
    String comment;
    Map<String,String> extra;

    public PaxTarEntry(String name) {
        super(name);
    }

    public PaxTarEntry(TarEntry entry) {
        super(entry);
        if (entry instanceof PaxTarEntry) {
            PaxTarEntry paxentry = (PaxTarEntry)entry;
            this.atime = paxentry.atime;
            this.charset = paxentry.charset;
            this.comment = paxentry.comment;
            this.extra = new HashMap<>(paxentry.extra);
        }
    }

    public long getAtime() {
        return atime;
    }

    public void setAtime(long atime) {
        this.atime = atime;
    }

    public String getCharset() {
        return charset;
    }

    public void setCharset(String charset) {
        this.charset = charset;
    }

    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    public Map<String, String> getExtra() {
        return extra;
    }

    public void setExtra(Map<String, String> extra) {
        this.extra = extra;
    }

    @Override
    byte[] headerBlock() {
        boolean namelong = false;
        if (name.length() <= 255
                && linkname.length() <= 100
                && size <= 077777777777L
                && mtime <= 077777777777L
                && atime <= 0
                && (comment == null || comment.isEmpty())
                && (extra == null || extra.isEmpty())) {
            try {
                return super.headerBlock();
            } catch (IndexOutOfBoundsException e) {
                if (!e.getMessage().equals("Name cannot be fit into header")) {
                    namelong = true;
                    throw e;
                }
            }
        }
        StringBuilder exthdr = new StringBuilder();
        if (name.length() > 255 || namelong) {
            exthdr.append(extHdrEntry("name", name));
        }
        return null;
    }
    
    private String extHdrEntry(String name, String value) {
        String hdrpart = String.format("%s=%s\n",name,value);
        int strlen = hdrpart.length() + 1;
        int lendgts = ((int)Math.log10(strlen)) + 1;
        int len = strlen + lendgts;
        String hdr = String.format("%d %s=%s\n",len,name,value);
        while (hdr.length() > len) {
            ++len;
            hdr = String.format("%d %s=%s\n",len,name,value);
        }
        return hdr;
    }
}
